package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.service.MenuService;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 权限管理service实现类
 * @author: Kerwin
 * @date: 2020/3/8-11:59
 * @Version: 1.0.0
 */
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private MenuService menuService;


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
//        3、在service第一步需要使用分页插件（告诉分页拦截器我们现在要查询第一页，每页要多少条）
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        Page<Permission> page = permissionDao.selectAll(queryPageBean.getQueryString());


        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Permission findById(Integer id) {


        return permissionDao.findById(id);
    }

    /**
     * 修改
     *
     * @param permission
     * @return void
     * @author Kerwin
     * @date 2020/3/9-17:33
     */
    @Override
    public Result edit(Permission permission) {

        Integer count = permissionDao.selectExcludeByPermissionId(permission);
        if (count > 0) {

            return Result.error("权限关键字已存在！！！");
        }
        permissionDao.updateByPrimaryKeySelective(permission);
        return Result.success("修改成功");
    }

    /**
     * 增加
     *
     * @param permission
     * @return com.itheima.entity.Result
     * @author Kerwin
     * @date 2020/3/9-17:34
     */
    @Override
    public Result add(Permission permission) {

        Integer count = permissionDao.selectByKeyword(permission.getKeyword());

        if (count > 0) {

            return Result.error("权限关键字已存在！！！");
        }

        permissionDao.insert(permission);

        return Result.success("添加成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return com.itheima.entity.Result
     * @author Kerwin
     * @date 2020/3/9-17:34
     */
    @Override
    public Result delete(Integer id) {

        Integer count = permissionDao.selectRoleAndPermissionByPermissionId(id);

        if (count > 0) {

            return Result.error("此权限还与角色存在关联，不可删除");
        }

        permissionDao.deleteByPrimaryKey(id);

        return Result.success("删除成功！！！");
    }

    @Override
    public Map<String,Object> findAll() {
        List<Permission> permissions = permissionDao.findAll();
        List<Menu> menus = menuService.findAll();
        Map<String, Object> map = new HashMap<>();
        map.put("permissions", permissions);
        map.put("menus", menus);
        return map;
    }
}
